# -*- coding: utf-8 -*-

import time
import os
from yyets import YYETS, VideoFormat, Download_Way_Map
from cli import parse_cli_args


import pretty


def print_res_info(yy: YYETS):
    basic = yy.get_resource_info()
    s_count, e_count = yy.get_total()
    infos = {
        "中文名称": basic.get("cnname", ""),
        "英文名称": basic.get("enname", ""),
        "别名": basic.get("aliasname", ""),
        "频道": basic.get("channel", "_") + " / " + basic.get("channel_cn", "_"),
        "地区": basic.get("area", "未知"),
        "总计": "{} 季，{} 集".format(s_count, e_count),
        "视频格式": ", ".join([f.value for f in yy.get_all_support_formats()]),
        "下载方式": ", ".join(
            [Download_Way_Map.get(w, "") for w in yy.get_all_support_ways()]
        ),
    }
    pretty.print_dict_as_table("", infos)


def check_args(yy: YYETS, args) -> bool:
    all_format = yy.get_all_support_formats()
    if args.format != VideoFormat.V_IDONTCARE and args.format not in all_format:
        pretty.print_error(
            "当前资源不支持该格式: {}，可以从以下格式中选择:\n· {}".format(
                args.format.value, "\n· ".join([x.value for x in all_format])
            )
        )
        return False

    return True


def get_res_url(yy: YYETS, args) -> list:
    pass
    # episodes = []
    # if episode == 0:
    #     episode = self.get_all_episodes(season_no, video_format)
    # else:
    #     episode = self.get_one_episode(video_format, season_no, episode)

    # if len(episode) == 0:
    #     return None

    # res = []
    # for e in episodes:
    #     resp.append(e.)


if __name__ == "__main__":
    pretty.print_fake_loading(10, 0.1)
    args = parse_cli_args()
    yy = YYETS(args.file)
    print_res_info(yy)
    if args.info:
        os._exit(0)

    if not check_args(yy, args):
        os._exit(1)

    # print(
    #     [
    #         e.get_url(WayFormat.W_IDONTCARE)
    #         for e in yy.get_all_episodes(1, VideoFormat.V_IDONTCARE)
    #     ]
    # )
    # print(yy.get_one_episode(args.format, 1, 1).get_name())
